<?php

namespace App\Http\Controllers;


use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;

class IndexController extends Controller
{
    public function index()
    {
        $lists = DB::table('notice')->limit(5)->orderBy('id','desc')->get();
        $lists = objectToArray($lists);
        /*foreach ($lists as $k=>$v){
            $lists[$k]['image'] = config('app.url').'/upload/'.$v['image'];
        }*/
        
        $banner = DB::table('banner')->limit(6)->orderBy('id','desc')->get();
        $banner = objectToArray($banner);
        foreach ($banner as $k=>$v){
            $banner[$k]['image'] = config('app.url').'/upload/'.$v['image'];
        }

        $data = [
            'notices'=>$lists,
            'banner'=>$banner,
        ];
        return response()->json(['code'=>0,'data'=>$data]);
    }

    public function addPlan(Request $request){

        $trip_plan_city = $request->input('trip_plan_city','');
        $arr = explode(',',$trip_plan_city);
        $trip_way = $request->input('trip_way',0);
        $trip_plan_way = $request->input('trip_plan_way',0);
        $user_id = $request->input('user_id',0);

        if (empty($user_id) || empty($trip_plan_city) || empty($trip_way) || empty($trip_plan_way) || count($arr)==0){
            return response()->json(['code'=>10001,'msg'=>'参数不全']);
        }

        $temp = [
            'trip_plan_city'=>$trip_plan_city,
            'user_id'=>$user_id,
            'trip_way'=>$trip_way,
            'trip_plan_way'=>$trip_plan_way,
            'created_at'=>date('Y-m-d H:i:s'),
            'updated_at'=>date('Y-m-d H:i:s'),
        ];

        $res = DB::table('travel_plan')->insertGetId($temp);
        $temp['id'] = $res;

        return response()->json(['code'=>0,'data'=>$temp]);

    }
    
    public function contact(){
        $data = DB::table('page')->where('id',1)->first();
        return response()->json(['code'=>0,'data'=>$data]);
    }
    
}